wordSize = 4;

// This is SUPER inefficient!!!! XXXXX
function matchesWordList(ww) {
    var wordList = [
        'able',
        'aid',
        'air',
        'ant',
        'art',
        'at',
        'be',
        'bean',
        'beat',
        'bee',
        'beet',
        'best',
        'bet',
        'bone',
        'cane',
        'can',
        'cat',
        'chat',
        'chin',
        'man',
        'mane',
        'map',
        'mare',
        'mast',
        'mat',
        'mate',
        'maze',
        'me',
        'men',
        'rat',
        'ring',
        'rose',
        'tail',
        'tale',
        'tar',
        'tub',
        'vase'
    ]
    var word = '';
    var p = ww.indexOf(' ');
    for (var i = 0; i < p; i++) {
        word += ww[i];
    }
    var matches = [];
    for (var i = 0; i < wordList.length; i++) {
        var w = wordList[i];
        var p = w.indexOf(word);
        if (p == 0 && word.length > 0) {
            matches.push(w);
            //alert("match: " + w);
        }
    }
    return matches;
}

function createAlphabetList() {
    var x = {};
    x[0] = ' ';
    for (var i = 0; i < 26; i++) {
        x[i+1] = String.fromCharCode(97 + i);
    }
    return x;
}

function createIndexList() {
    var x = {};
    x[' '] = 0;
    for (var i = 1; i < 27; i++) {
        x[String.fromCharCode(96 + i)] = i;
    }
    return x;
}

alphabet = createAlphabetList();
alphaindices = createIndexList();
    
function letterOfIndex(i) {
    return alphabet[i];
}

function indexOfLetter(c) {
    return alphaindices[c];
}

function getMatches() {
    var results = SpinningWheel.getSelectedValues();
    var word = results.values.join('');
    var matches = matchesWordList(word);
    return matches;
}

function updateOnScroll() {
    var matches = getMatches();
    var match = '    ';
    if (matches.length > 0) {
        match = matches[0];
    }
    //alert(match);
    var next = SpinningWheel.activeSlot + 1;
    var j = next;
    for (var ii = next; ii<wordSize; ii++) {
        var m = 0;
        if (j < match.length) {
            m = indexOfLetter(match[j]);
        }
        j++;
        SpinningWheel.scrollToValue(ii, m);
    }
}

function openWords() {
    SpinningWheel.addSlot(alphabet, 'right', 2);
    SpinningWheel.addSlot(alphabet, 'right', 5);
    SpinningWheel.addSlot(alphabet, 'right', 0);
    SpinningWheel.addSlot(alphabet, 'right', 0);
    SpinningWheel.setCancelAction(cancel);
    SpinningWheel.setDoneAction(done);
    SpinningWheel.setScrollAction(updateOnScroll);
    SpinningWheel.open();    
}

function openBirthDate() {
    var now = new Date();
    var days = { };
    var years = { };
    var months = {
        1: 'Gen', 
        2: 'Feb', 
        3: 'Mar', 
        4: 'Apr', 
        5: 'May', 
        6: 'Jun', 
        7: 'Jul', 
        8: 'Aug', 
        9: 'Sep', 
        10: 'Oct', 
        11: 'Nov', 
        12: 'Dec'
    };
	
    for( var i = 1; i < 32; i += 1 ) {
        days[i] = i;
    }

    for( i = now.getFullYear()-100; i < now.getFullYear()+1; i += 1 ) {
        years[i] = i;
    }

    SpinningWheel.addSlot(years, 'right', 1999);
    SpinningWheel.addSlot(months, '', 4);
    SpinningWheel.addSlot(days, 'right', 12);
	
    SpinningWheel.setCancelAction(cancel);
    SpinningWheel.setDoneAction(done);
	
    SpinningWheel.open();
}

function openOneSlot() {
    SpinningWheel.addSlot({
        1: 'Ichi', 
        2: 'Ni', 
        3: 'San', 
        4: 'Shi', 
        5: 'Go'
    });
	
    SpinningWheel.setCancelAction(cancel);
    SpinningWheel.setDoneAction(done);
	
    SpinningWheel.open();
}

function done() {
    var results = SpinningWheel.getSelectedValues();
    var word = results.values.join('');
    var r = 'values: ' + results.values.join(' ') + '<br />keys: ' + results.keys.join(', ');   
    $('#result').html(r);

    var matches = getMatches();
    var q = matches.join(", ");
    q = "matches: " + q;
    $('#matches').html(q);    
    
    var yw = '<p id="added">Your word: <input value="'+word+'" /></p>';
    $('#added').html(yw);
}

function cancel() {
    document.getElementById('result').innerHTML = 'cancelled!';
}

window.addEventListener('load', function(){ setTimeout(function(){ window.scrollTo(0,0); }, 100); }, true);

